package com.isunland.managesystem.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.CustomerDialog;

public class RequisitionDataStatusDialogFragment extends DialogFragment {
  protected static final String EXTRA_DATA_STATUS =
      "com.isunland.managesystem.ui.EXTRA_DATA_STATUS";
  public static final String VALUE_NEW = "new";
  public static final String VALUE_WAITCHECK = "waitCheck";
  public static final String EXTRA_CHECKPASS = "checkPass";
  public static final String VALUE_ABORT = "abort";
  private int[] choiceIds = new int[] {
      R.string.all, R.string.draft, R.string.checking, R.string.checkapprovve, R.string.stopup
  };
  private String[] choiceStrings = new String[choiceIds.length];

  @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new AlertDialog.Builder(getActivity()).setSingleChoiceItems(getChoiceName(), 0,
        new OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {

            String dataStatus;
            switch (which) {
              case 0:
                dataStatus = "";
                break;
              case 1:
                dataStatus = VALUE_NEW;
                break;
              case 2:
                dataStatus = VALUE_WAITCHECK;
                break;
              case 3:
                dataStatus = EXTRA_CHECKPASS;
                break;
              case 4:
                dataStatus = VALUE_ABORT;
                break;

              default:
                dataStatus = "";
                break;
            }

            if (getTargetFragment() == null) return;

            CustomerDialog customerDialog = new CustomerDialog(getChoiceName()[which], dataStatus);
            Intent i = new Intent();
            i.putExtra(EXTRA_DATA_STATUS, customerDialog);
            getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);

            dialog.dismiss();
          }
        }).create();
  }

  private String[] getChoiceName() {
    for (int i = 0; i < choiceIds.length; i++) {

      choiceStrings[i] = getResources().getString(choiceIds[i]);
    }

    return choiceStrings;
  }
}
